#include <iostream>
#include <limits>

using namespace std;

const int INF = std::numeric_limits<int>::max();

#define M   4
#define N   4

bool Yong(int matrix[][N], int search) {
    int i, j;
    
    i = 0, j = N-1;

    while(i >= 0 && i < M && j >= 0 && j < N) {
        if (matrix[i][j] > search)
            j--;
        else if (matrix[i][j] < search)
            i++;
        else
            return true;
    }
    return false;
}

int
main() {
    int matrix[M][N] = {
        {1, 2, 8, 9},
        {2, 4, 9, 12},
        {4, 7, 10, 13},
        {6, 8, 11, 15},
    };

    for (int i = 0; i < 16; ++i) {
        cout << i << " : " << 
            (Yong(matrix, i) ? "存在" : "不存在") 
            << endl;
    }
    return 0;
}

